import {umiFileDownload} from '@/utils/fileUtil';
import {apiPrefixDispose} from '@/utils/format';
import {request} from '@umijs/max';


/**
 * 分页查询
 */
export async function page<T = any>(body: API.DbBackupPage) {
    return request<API.ResultPageVO<API.DbBackup>>(apiPrefixDispose('/dbBackup/page'), {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
      },
      data: body,
    });
  }

/**
 * ssh集合
 */
export async function listSSH<T = any>() {
    return request<API.ResultVO<API.DbBackup[]>>(apiPrefixDispose('/dbBackup/lists'), {
      method: 'GET',
    });
  }

/**
 * 根据ID获取详情
 */
export async function info<T = any>(id: number) {
    return request<API.ResultVO<API.DbBackup>>(apiPrefixDispose(`/dbBackup/${id}`), {
      method: 'GET',
    });
  }


  /**
   * 新增服务器ssh连接信息
   */
  export async function append<T = any>(
    body: API.DbBackupAdd,
    options?: { [key: string]: any },
  ) {
    return request<API.ResultVO<string>>(apiPrefixDispose(`/dbBackup/append`), {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
      },
      data: body,
      ...(options || {}),
    });
  }


/**
 * 删除服务器ssh连接信息
 */
export async function deleteByIds<T = any>(
    codes: Array<number>,
    options?: { [key: string]: any },
  ) {
    return request<API.ResultVO<string>>(apiPrefixDispose(`/dbBackup/delete`), {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
      },
      data: codes,
      ...(options || {}),
    });
  }


/**
 * 编辑服务器ssh连接信息
 */
export async function edit<T = any>(body: API.DbBackupEdit) {
    return request<API.ResultVO<string>>(apiPrefixDispose('/dbBackup/edit'), {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
      },
      data: body,
    });
  }


/**
 * 支持的数据库类型
 */
export async function dbTypes<T = any>() {
  return request<API.ResultVO<Array<API.DbType>>>(apiPrefixDispose('/dbBackup/dbTypes'), {
    method: 'GET',
  });
}


/**
 * 备份
 */
export async function backup<T = any>(id:number) {
  return request<API.ResultVO<string>>(apiPrefixDispose('/dbBackup/backup'), {
    method: 'GET',
    params: { 'id': id },
  });
}


/**
 * 查询备份的文件
 */
export async function findBackupFiles<T = any>(id:number) {
  return request<API.ResultVO<API.SshRemoteFile[]>>(apiPrefixDispose('/dbBackup/files'), {
    timeout: -1,
    method: 'GET',
    params: { 'id': id },
  });
}


/**
 * 下载备份的文件
 */
export async function downloadBackupFiles<T = any>(id:number,fileName:string) {
    umiFileDownload({
      url: apiPrefixDispose('/dbBackup/files/download'),
      params: { id: id, fileName: fileName },
      fileName: fileName,
      onProgress: (percent) => {
        console.log(`Download progress: ${percent}%`);
        // setProgress(percent); // Update progress state
      },
    });
}



/**
 * 删除备份的文件
 */
export async function deleteBackupFiles<T = any>(id:number,fileName:string) {
  return request<API.ResultVO<API.SshRemoteFile[]>>(apiPrefixDispose('/dbBackup/files/delete'), {
    timeout: -1,
    method: 'GET',
    params: { 'id': id ,'fileName': fileName },
  });
}
